package com.yanggu.spark.core.rdd.action

import org.apache.spark.{SparkConf, SparkContext}

//Action算子-Fold-简化版的Aggregate分区内的数据和分区间的数据处理逻辑一致
object RDD34_Fold {

  def main(args: Array[String]): Unit = {

    //1. 创建sparkConf配置对象
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("spark")

    //2. 创建spark上下文对象
    val sparkContext = new SparkContext(sparkConf)

    //3. 从内存中创建RDD
    val value = sparkContext.makeRDD(List[Int](1, 2, 3, 4, 5), 8)

    //4. Fold算子。行动算子
    //函数声明
    //def fold(zeroValue: T)(op: (T, T) => T): T
    val sum = value.fold(0)(_ + _)

    //5. 打印
    println(sum)

    //6. 释放资源
    sparkContext.stop()
  }

}
